﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Basic.Part2
{

    public interface IObjectBuilder
    {
        /// <summary>
        /// 创建类型实例
        /// </summary>
        /// <typeparam name="T">返回类型</typeparam>
        /// <param name="args">构造参数</param>
        /// <returns>指定类型T的实例</returns>
        T BuildUp<T>(object[] args);

        /// <summary>
        /// 创建类型实例
        /// <remarks>该方法仅适用于有无参数构造函数的类型</remarks>
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        T BuildUp<T>() where T : new();

        /// <summary>
        /// 按照目标返回的类型，加工指定类型名称对应的类型实例。
        /// 目标类型可以为接口，抽象类等抽象类型，typeName一般为实体类名称。
        /// </summary>
        /// <typeparam name="T">目标返回的类型。</typeparam>
        /// <param name="typeName">类型名称</param>
        /// <returns>按照目标返回类型加工友好的一个实例</returns>
        T BuildUp<T>(string typeName);

        /// <summary>
        /// 按照目标类型，通过调用指定名称类型的构造函数，生成目标类型实例
        /// </summary>
        /// <typeparam name="T">目标返回的类型</typeparam>
        /// <param name="typeName">类型名称</param>
        /// <param name="args">构造函数参数</param>
        /// <returns>按照目标返回类型加工好的一个实例</returns>
        T BuildUp<T>(string typeName, object[] args);
    }



    public class TypeCreator : IObjectBuilder
    {
        public T BuildUp<T>() where T : new()
        {
            return Activator.CreateInstance<T>();
        }

        public T BuildUp<T>(string typeName)
        {
            var type = Type.GetType(typeName);
            if (type != null)
                return (T)Activator.CreateInstance(type);
            throw new NullReferenceException();
        }

        public T BuildUp<T>(object[] args)
        {
            var result = Activator.CreateInstance(typeof(T), args);
            return (T)result;
        }

        public T BuildUp<T>(string typeName, object[] args)
        {
            var type = Type.GetType(typeName);
            if (type == null) throw new NullReferenceException();
            var result = Activator.CreateInstance(type, args);
            return (T)result;
        }
    }
}
